package cn.big.bigd.biocode.controller;


import cn.big.bigd.biocode.dao.CategoryDAO;
import cn.big.bigd.biocode.dao.OrganizationDAO;
import cn.big.bigd.biocode.dao.ToolDAO;
import cn.big.bigd.biocode.dao.UserDAO;
import cn.big.bigd.biocode.dao.vocabulary.*;
import cn.big.bigd.biocode.dto.Result;
import cn.big.bigd.biocode.model.Organization;
import cn.big.bigd.biocode.model.User;
import cn.big.bigd.biocode.service.PmidService;
import cn.big.bigd.biocode.service.ToolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @ClassName: UserController
 * @Description: TODO   
 * @Author: FanWang
 * @CreateDate: 2018/1/10 下午10:06
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018/1/10
 **/

@RestController
@RequestMapping("api")
public class ApiController {




    @Autowired
    ToolDAO toolDAO;
    @Autowired
    UserDAO userDAO;
    @Autowired
    ToolService toolService;

    @Autowired
    OrganizationDAO organizationDAO;


    @Autowired
    TechDAO techDAO;

    @Autowired
    DataTypeDAO dataTypeDAO;
    @Autowired
    PlatformDAO platformDAO;
    @Autowired
    TaskTypeDAO taskTypeDAO;
    @Autowired
    ToolTypeDAO toolTypeDAO;
    @Autowired
    UserInterfaceDAO userInterfaceDAO;
    @Autowired
    CategoryDAO categoryDAO;

    @RequestMapping(value = "emails")
    public List<User> emails(String q) {
        List<User> users =userDAO.findByEmailContains(q);
        return users;
    }

    @RequestMapping(value = "organizations")
    public List<Organization> organizations(String q) {
        List<Organization> organizations =organizationDAO.findByNameContains(q);
        return organizations;
    }

    @RequestMapping(value = "departments")
    public List<Organization> departments(String name) {
        List<Organization> organizations =organizationDAO.findByName(name);
        return organizations;
    }


    @RequestMapping(value = "pmidSearch")
    public Result pmidSearch(String pmid) {
        return PmidService.getPublicationByPmidJson(pmid);
    }

    @RequestMapping(value = "doiSearch")
    public Result doiSearch(String doi) {
        return PmidService.getPublicationByDoiJson(doi);
    }


    @RequestMapping(value = "toolsDistribute")
    public List<Map<String, Object>> toolsDistribute() {
        return toolService.findToolDistribute();
    }

    @RequestMapping(value = "techDistribute")
    public List<Map<String, Object>> techDistribute() {
        String sql ="SELECT vocabularies.name, count(*) AS y FROM vocabularies\n" +
                "  JOIN tools_vocabularies ON vocabularies.id=tools_vocabularies.vocabulary_id\n" +
                "  JOIN tools ON tools.id=tools_vocabularies.tool_id\n" +
                "WHERE vocabularies.type='Tech'\n" +
                "GROUP BY vocabularies.id\n" +
                "ORDER BY y;";
        return toolService.findMapListBySql(sql);
    }

    @RequestMapping(value = "downloadStat")
    public Object downloadStat(Integer id) {
        String sql ="SELECT releases.version, YEAR(date) as year, MONTH(date) as month, sum(visit_tracks.count) as count FROM visit_tracks\n" +
                "  INNER JOIN releases ON visit_tracks.target_id=releases.id\n" +
                "  INNER JOIN tools ON tools.id=releases.tool_id\n" +
                "WHERE tools.id = "+id+" AND visit_tracks.target_type='release_download'\n" +
                "GROUP BY visit_tracks.target_id, year,month";



        List<Map<String, Object>> list = toolService.findMapListBySql(sql);
        System.out.println(list.size());
        List<Map<String, Object>> series = new ArrayList<>();

        List<String > dates = new ArrayList<>();
        List<String > releases = new ArrayList<>();
        for (Map<String, Object> stringObjectMap : list) {
            System.out.println("------::"+stringObjectMap.toString());
            String year =stringObjectMap.get("year").toString();
            String month =stringObjectMap.get("month").toString();
            if(month.length()==1){
                month=0+""+month;
            }
            String date =year+"-"+month;
            if(!dates.contains(date)){
                dates.add(date);
            }
            stringObjectMap.put("date",date);
            String release =stringObjectMap.get("version").toString();
            if(!releases.contains(release)){
                releases.add(release);
            }
        }
        Collections.sort(dates, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        for (int i = 0; i < releases.size(); i++) {
            List<Integer> datas = new ArrayList<>();
            String r =releases.get(i);
            Map<String, Object> seria =new HashMap<>();
            seria.put("name",r);
            for (int j = 0; j < dates.size(); j++) {
                boolean f=false;
                for (Map<String, Object> stringObjectMap : list) {
                    String date =stringObjectMap.get("date").toString();
                    String version =stringObjectMap.get("version").toString();
                    Integer count =Integer.valueOf(stringObjectMap.get("count").toString());
                    System.out.println(stringObjectMap.toString());
                    if(date.equals(dates.get(j))&&r.equals(version)){
                        datas.add(count);
                        f=true;
                    }

                }
                if(!f){
                    datas.add(0);
                }
            }

            seria.put("data",datas);
            series.add(seria);



        }
        Map m =new HashMap();
        m.put("dates",dates);
        m.put("series",series);
        return  m;

    }

    @RequestMapping(value = "toolDistribute")
    public List<Map<String, Object>> toolDistribute(Integer id) {
        String sql ="SELECT visit_tracks.country, sum(visit_tracks.count) FROM visit_tracks\n" +
                "  INNER JOIN releases ON visit_tracks.target_id=releases.id\n" +
                "  INNER JOIN tools ON tools.id=releases.tool_id\n" +
                " WHERE tools.id="+id+" AND visit_tracks.target_type='release_download'\n" +
                " GROUP BY visit_tracks.country";
        return toolService.findMapListBySql(sql);
    }
}
